/**
 * 打骰子组件
 * Created by duwei on 2018/4/26.
 */
var Ma_ThrowDiceWindow = Window.extend({
    _ccsFileName : majiang_hn.res.Ma_ThrowDiceWindow_json,
    /**
     * 初始化
     * @public
     */
    initWindow: function (room,title, dice, paiIdA, paiIdB, cb) {
        var self = this;
        self.room = room;
        this.throwDice(title, dice, paiIdA, paiIdB, cb);
    },
    playThrowDice:function (sprite, k, endCb) {
        cc.spriteFrameCache.addSpriteFrames(majiang_hn.res.dice_plist);
        var animFrames = [];
        for (var i = 0; i < 4; i++) {
            var frame = cc.spriteFrameCache.getSpriteFrame('dice_Action_' + i + '.png');
            animFrames.push(frame);
        }
        var animation = new cc.Animation(animFrames, 0.04);
        var action = cc.animate(animation);
        sprite.runAction(cc.sequence(
            action.repeat(2),
            cc.callFunc(function () {
                setSpriteFrameByName(sprite, 'dice_' + k + '.png', majiang_hn.res.dice_plist);
            }),
            cc.delayTime(0.4),
            cc.callFunc(function () {
                if (endCb)
                    endCb();
            })
        ));
    },
    /**
     * 开杠扔骰子动画
     */
    throwDice: function (title, dice, paiIdA, paiIdB, cb) {
        var self = this;
        title = title || '';
        if (dice < 1 || dice > 6)
            return;
        self._nodeList['root'].setVisible(true);
        var paiA = self._nodeList['root.a0'];
        var paiB = self._nodeList['root.a1'];
        paiA.setVisible(false);
        paiB.setVisible(false);
        self._nodeList['root.lb_title'].setString(title);
        self._nodeList['root.lb_title'].setVisible(true);
        self.playThrowDice(self._nodeList['root.dice1'], Math.floor(Math.random() * 6) + 1);
        self.playThrowDice(self._nodeList['root.dice'], dice, function () {
            var paiAName = self.room.getPaiNameByRowAndId(2, paiIdA, true, false);
            var paiBName = self.room.getPaiNameByRowAndId(2, paiIdB, true, false);
            setSpriteFrameByName(paiA, paiAName, majiang_hn.res.pai_hn_plist);
            setSpriteFrameByName(paiB, paiBName, majiang_hn.res.pai_hn_plist);
            paiA.setVisible(true);
            paiB.setVisible(true);
            setTimeout(function () {
                self.removeFromParent(true);
                network.start();
                if (cb)
                    cb();
            }, 500);
        });
    }
});